﻿using AntMovement.Core;
using AntMovement.Core.Entity;
using AntMovement.Core.Enum;
using AntMovement.Core.IParameter;
using AntMovement.Core.Tool;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace AntMovement.Menu.Device.ViewModels
{
    public class AxisSettingsViewModel : BindableBase
    {
        public IEnumerable<CustomZeroModeType> Foods
        {
            get
            {
                foreach (CustomZeroModeType c in (CustomZeroModeType[])Enum.GetValues(typeof(CustomZeroModeType)))
                {
                    yield return c;
                }
            }
        }
        public IEnumerable<Core.Enum.MotionType> MotionNames
        {
            get {
                foreach (Core.Enum.MotionType c in (Core.Enum.MotionType[])Enum.GetValues(typeof(Core.Enum.MotionType)))
                {
                    yield return c;
                }
            }
        }
        private ObservableCollection<Axis> _AxisDataSet;

        public ObservableCollection<Axis> AxisDataSet
        {
            get { return _AxisDataSet; }
            set { SetProperty(ref _AxisDataSet, value); }
        }

        public AntMovement.Parameter.AxisParameter AxisP { get; set; }
        private IDialogHost _dialogHost;
        public AxisSettingsViewModel(IAxisParameter axisParameter, IDialogHost dialogHost)
        {
            AxisP = (Parameter.AxisParameter)axisParameter;
            AxisDataSet = new ObservableCollection<Axis>(AxisP.AxisDataSet);
            _dialogHost = dialogHost;
        }

        /// <summary>
        /// 添加轴
        /// </summary>
        public DelegateCommand BasicSettingAdd => new DelegateCommand(() =>
        {
            AxisDataSet.Add(new Core.Entity.Axis()
            {
                Name = "新建轴",
                Status = new Core.Entity.AxisStatus(),
                UnitConversion = new AxisUnitConversion(),
                AxisOrderly = (ushort)(AxisDataSet.Count + 1),
                AxisID = (ushort)(AxisDataSet.Count > 0 ? AxisDataSet[AxisDataSet.Count - 1].AxisID + 1 : 0),
                IP = AxisDataSet.Count > 0 ? AxisDataSet[AxisDataSet.Count - 1].IP : "101",
                Parameter = new Core.Entity.AxisParameter(),
                CardNo = 0,
                MotionName = "",
                Type = Core.Enum.MotionType.SolidTech_E_SerialCard,
            });
        });

        /// <summary>
        /// 保存参数
        /// </summary>
        public DelegateCommand BasicSettingSave => new DelegateCommand(() =>
        {
            if (_copy == null)
            {
                _copy = ConversionHelper.CopyClass<Axis, Axis>();
            }
            foreach (Axis item in AxisDataSet)
            {
                var model = AxisP.AxisDataSet.FirstOrDefault(x => x.AxisOrderly == item.AxisOrderly && x.AxisID == item.AxisID);
                if (model != null)
                {
                    _copy.Invoke(item, model);
                }
                else
                {
                    AxisP.AxisDataSet.Add(item);
                }
            }
            if (AxisP.Save())
            {
                _ = _dialogHost.DialogShow("保存成功", LogLevel.Success, isOperation: true, PopUpType.Global);
            }
            else
            {
                _ = _dialogHost.DialogShow("保存失败", LogLevel.Error, isOperation: true, PopUpType.Global);
            }
        });

        /// <summary>
        /// 清空轴参数
        /// </summary>
        public DelegateCommand BasicSettingDelete => new DelegateCommand(() =>
        {
            AxisDataSet.Clear();
            AxisP.AxisDataSet.Clear();
            AxisP.Save();
        });

        private Action<Axis, Axis> _copy;
        /// <summary>
        /// 选中删除
        /// </summary>
        public DelegateCommand<DataGrid> InputSelectDelete => new DelegateCommand<DataGrid>(sender =>
        {
            if (_copy == null)
            {
                _copy = ConversionHelper.CopyClass<Axis, Axis>();
            }
            if (sender.SelectedItems == null || sender.SelectedItems.Count == 0)
            {
                return;
            }
            List<Axis> axisList = new List<Axis>();
            foreach (Axis item in sender.SelectedItems)
            {
                Axis model = new Axis();
                _copy.Invoke(item, model);
                axisList.Add(model);
            }
            foreach (Axis item in axisList)
            {
                var model = AxisP.AxisDataSet.FirstOrDefault(x => x.AxisOrderly == item.AxisOrderly && x.AxisID == item.AxisID);
                if (model != null)
                {
                    AxisP.AxisDataSet.Remove(model);
                }
            }
            for (int i = 0; i < AxisP.AxisDataSet.Count; i++)
            {
                AxisP.AxisDataSet[i].AxisOrderly = (ushort)(i + 1);
            }
            AxisDataSet.Clear();
            AxisP.Save();
            AxisDataSet = new ObservableCollection<Axis>(AxisP.AxisDataSet);
        });
    }
}
